#include "LayerStack.hpp"

namespace Kriko {
	LayerStack::~LayerStack()
	{
		for (Layer* layer : m_Layers)
		{
			layer->OnDetach();
			delete layer;
		}
	}

	void LayerStack::PopLayer(Layer* p_Layer)
	{
		auto it = std::find(m_Layers.begin(), m_Layers.begin() + m_insertIndex, p_Layer);
		if (it != m_Layers.begin() + m_insertIndex)
		{
			p_Layer->OnDetach();
			m_Layers.erase(it);
			--m_insertIndex;
		}
	}

	void LayerStack::PushLayer(Layer* p_Layer)
	{
		m_Layers.emplace(m_Layers.begin() + m_insertIndex, p_Layer);
		++m_insertIndex;
	}

	void LayerStack::PopOverLayer(Layer* p_OverLayer)
	{
		auto it = std::find(m_Layers.begin()+m_insertIndex,m_Layers.end(),p_OverLayer);
		if (it != m_Layers.end())
		{
			p_OverLayer->OnDetach();
			m_Layers.erase(it);
		}
	}

	void LayerStack::PushOverLayer(Layer* p_OverLayer)
	{
		m_Layers.emplace_back(p_OverLayer);
	}
}